package com.le.tester.book.concurrentchallenge.performanceandscalability11;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * createTime：2025-04-29 10:19
 * description：减少锁持有的时间
 */
public class BetterAttributeStore {

    private final Map<String, String> attributes = new HashMap<>();

    public boolean userLocationMatches(String name, String regex) {
        String key = "user." + name + ".location";
        String location;

        synchronized (this) {
            location = attributes.get(key);
        }
        if (location == null) {
            return false;
        } else {
            return Pattern.matches(regex, location);
        }

    }
}
